package atm.example;

import java.util.*;

/**
 * author:yan
 * function:账号管理：
 *    1. 账户管理
 *    2. 修改登录密码
 *    3. 注销
 *    4. 退出登录
 * date:2025-9-29
 *  *    6. 添加账户
 *  *    7. 账户切换
 */
public class User {
    private List<Account> accountList = new LinkedList<>();
    private String userPasswd;
    private String userName;
    private String firstName;
    public void setInfo(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        userName = sc.next();
        System.out.println("请输入密码");
        userPasswd = sc.next();
        System.out.println("请输入您的姓氏");
        firstName = sc.next();
        System.out.println("信息初始化成功");
    }
    public List<Account> getAccountList() {
        return accountList;
    }
    public String generateId(){
        Random rm = new Random();
        String card = "";
        for(int i = 0;i < 11;i++){
            card += rm.nextInt(10);
        }
        return card;
    }
    public void addAccount(Bank bank){
        Scanner sc = new Scanner(System.in);
        if(accountList.size() == 2){
            System.out.println("您的账户已达上限，请删除后再进行添加");
        }else{
            while(true){
                System.out.println("请输入你的交易密码");
                Account account = new Account();
                String transactionPasswd = sc.next();
                String cardId = generateId();
                if(bank.checkIdCard(cardId)){
                    account.setTransactionPasswd(transactionPasswd);
                    account.setIdCard(cardId);
                }
                accountList.add(account);
                System.out.printf("账户添加成功，卡号为：%s\n", cardId);
                return;
            }
        }
    }
    public boolean checkUserPasswd(String passwd) {
        return passwd.equals(userPasswd);
    }

    public String getFirstName() {
        return firstName;
    }

    public String getUserName(){
        return userName;
    }
    public void userManagePage(Bank topBank){
        Scanner sc = new Scanner(System.in);
        System.out.printf("欢迎%s，您可以使用以下功能:\n", userName);
        while(true){
            System.out.println("--------ATM系统----------");
            int funcId = 0;
            System.out.println("1.账户管理\n2.添加账户\n3.修改登录密码\n4.注销\n5.退出登录");
            funcId = Integer.parseInt(sc.next());
            Out:
            switch (funcId){
                case 1:{
                    if(accountList.size() > 0){
                        System.out.printf("当前账号共有%d个账户\n", accountList.size());
                        int order = 0;
                        for(Account account : accountList){
                            order++;
                            System.out.printf("%d卡号为：" + account.getIdCard() + "\n", order);
                        }
                        System.out.println("请输入你要操作的账户序号:");
                        order = sc.nextInt();
                        sc.nextLine();
                        System.out.println("请输入该账户的交易密码");

                        int times = 3;
                        while(times > 0){
                            String transPasswd= sc.next();
                            times--;
                            if(accountList.get(order - 1).checkTransPasswd(transPasswd)){
                                accountList.get(order - 1).accountManage(topBank);
                                break;
                            }
                            else{
                                System.out.printf("交易密码有误，您还可以输入%d次\n", times);
                            }
                        }
                    }
                    else{
                        System.out.println("当前账号无账户");
                    }
                    break;
                }
                case 2:{
                    addAccount(topBank);
                    break;
                }
                case 3:{
                    changeUserPasswd();
                    break;
                }
                case 4:{
                    for(Account account : accountList){
                        if(account.getBalance() != 0){
                            System.out.println("余额还未清空，无法注销，清空后再试");
                            break Out;
                        }
                    }
                    topBank.deleteUser(this);
                    return;
                }
                case 5:{
                    System.out.println("您已退出当前账号，欢迎下次使用");
                    return;
                }
                default:{
                    System.out.println("您输入了错误的功能编号，请重新输入");
                    break;
                }
            }
        }

    }
    public void changeUserPasswd(){
        System.out.println("请输入密码");
        Scanner sc = new Scanner(System.in);
        String passwd = sc.next();
        if(checkUserPasswd(passwd)){
            System.out.println("请输入修改后的密码:");
            passwd = sc.next();
            userPasswd = passwd;
            System.out.println("修改密码成功");
        }
        else{
            System.out.println("修改失败，您输入的密码有误");
        }
    }

}
